exports.empty = function (str) {
  if (str === 'undefined' || !str || !/[^\s]/.test(str)) {
    return true
  } else {
    return false
  }
}

exports.list2tree = (data, parent) => {
  // const data = Object.assign([], list)
  const dataObj = {}

  data.forEach((item) => {
    item.children = []
    dataObj[item._id] = item
  })

  const res = data.filter((item) => {
    if (typeof (item.parent) === 'undefined' || item.parent === parent) {
      return true
    } else {
      item.parent && dataObj[item.parent].children.push(item)
      return false
    }
  })

  for (const key in dataObj) {
    // dataObj[key].hasChildren = true
    if (dataObj[key].children.length === 0) {
      delete dataObj[key].children
      // dataObj[key].hasChildren = false
    }
  }
  return {
    treeData: res,
    objData: dataObj
  }
}
